package com.robot.service.impl;

import com.robot.common.enums.ResponseEnum;
import com.robot.common.exception.BusinessException;
import com.robot.dao.dto.RoleDto;
import com.robot.dao.entity.Role;
import com.robot.dao.mapper.RoleMapper;
import com.robot.service.RoleService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void insertRole(RoleDto roleDto) {
        if(StringUtils.isBlank(roleDto.getRoleCode())||StringUtils.isBlank(roleDto.getRoleName())){
            throw new BusinessException(ResponseEnum.ILLEGAL_PARAMETER.getCode(), "角色名/角色编码不能为空！");
        }
        Role role=roleMapper.searchByRoleCode(roleDto);
        if(!ObjectUtils.isEmpty(role)){
            throw new BusinessException(ResponseEnum.ILLEGAL_PARAMETER.getCode(), "角色编码已存在");
        }
        Role roleNew=Role.builder()
                .roleCode(roleDto.getRoleCode())
                .roleName(roleDto.getRoleName())
                .build();

        roleMapper.insert(roleNew);

    }
}
